package com.vrv.im.control;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import com.vrv.im.domain.ProjectBean;
import com.vrv.im.domain.RoleBean;
import com.vrv.im.domain.UserBean;
import com.vrv.im.domain.UserRoleBean;
import com.vrv.im.service.ProjectService;
import com.vrv.im.service.RoleService;
import com.vrv.im.service.UserRoleService;
import com.vrv.im.service.UserService;
import com.vrv.im.util.Util;
import com.vrv.im.vo.BindProjectVo;
import com.vrv.im.vo.ProjectVo;

@Controller
@RequestMapping("/bindProject")
public class ProjectBindController {

    @Autowired
    private UserService userService;

    @Autowired
    private RoleService roleService;

    @Autowired
    private ProjectService projectService;

    @Autowired
    private UserRoleService userRoleService;

    /**
     * <ol>
     * 根据不同的角色去处理项目
     * <li>首先根据用户角色和项目查询出项目列表</li>
     * </ol>
     *
     * @return
     */
    @RequestMapping("/myProject")
    public ModelAndView myProject(@ModelAttribute("voModel") UserBean userBean) {

        if (userBean == null || !StringUtils.hasText(userBean.getOpenId())
                || !StringUtils.hasText(userBean.getToken())) {
            ModelAndView mav = new ModelAndView("userbind/bind");
            mav.addObject("voModel", new UserBean());
            mav.addObject("type", "error");
            mav.addObject("errotMsg", "请传递参数openID，tocken");
            return mav;
        } else {
            userBean = userService.getUserByTokenID(userBean);
            if (null == userBean || !StringUtils.hasText(userBean.getId() + "")) {
                return new ModelAndView(
                        "redirect:/bindProject/goToBindProject.do");
            } else {
                List<ProjectVo> bindProjects = userRoleService
                        .getBindProjectByUserID(userBean.getId());
                ModelAndView mav = new ModelAndView("projectbind/myProject");
                mav.addObject("voModel", bindProjects);
                mav.addObject("userBean", userBean);
                return mav;
            }
        }
    }

    @RequestMapping("/show")
    public ModelAndView showProject(
            @ModelAttribute("voModel") BindProjectVo bindProjectVo) {
        ModelAndView mav = new ModelAndView("projectbind/show");
        ProjectBean projectBean = projectService.getProjectExtVoByID(
                bindProjectVo.getUserID(), bindProjectVo.getProjectID());
        mav.addObject("voModel", projectBean);
        return mav;
    }

    @RequestMapping("/goToBindProject")
    public ModelAndView gotoPage() {
        // 进入绑定界面
        List<UserBean> userList = userService.getAllUser();
        List<RoleBean> roleList = roleService.getAllRole();
        List<ProjectBean> projectList = projectService.getAllProject();
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("userList", userList);
        map.put("roleList", roleList);
        map.put("projectList", projectList);
        return new ModelAndView("projectbind/bind", map);
    }

    @RequestMapping("/bind")
    public ModelAndView bind(
            @ModelAttribute("projectVo") BindProjectVo projectVo) {
        long roleID = userRoleService.getRoleIDByUserID(projectVo.getUserID(),
                projectVo.getProjectID());
        // 如果绑定过了就不用绑定了
        if (roleID <= 0)
            userRoleService.add(projectVo);
        if (projectVo.getRoleID() == 1) {
            // 绑定的角色是业主,进入解绑界面
            List<ProjectVo> bindProjects = userRoleService
                    .getBindProjectByUserID(projectVo.getUserID());
            Map<String, Object> map = new HashMap<String, Object>();
            map.put("bindProjects", bindProjects);
            return new ModelAndView("projectbind/unbind", map);
        } else {
            // 如果是其他角色则进入绑定界面继续绑定
            return new ModelAndView("redirect:projectbind/goToBindProject.do");
        }
    }

    @RequestMapping("/goToUnBind")
    public ModelAndView goToUnBindPage() {
        List<ProjectVo> list = userRoleService.getAllBindProject();
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("bindProjects", list);
        return new ModelAndView("/projectbind/unbind", map);
    }

    @RequestMapping("/unbind")
    public ModelAndView unBind(@RequestParam("str") String str) {
        List<UserRoleBean> delList = new ArrayList<UserRoleBean>();
        List<String> list = Util.getSplit(str, ",");
        if (list != null && list.size() > 0) {
            for (String s : list) {
                if (s != null && !"".equals(s.trim())) {
                    List<String> subList = Util.getSplit(s, "-");
                    if (subList != null && subList.size() == 3) {
                        UserRoleBean b = new UserRoleBean();
                        long proID = Long.valueOf(subList.get(0).trim());
                        b.setProjectID(proID);
                        long roleID = Long.valueOf(subList.get(1).trim());
                        b.setRoleID(roleID);
                        long userID = Long.valueOf(subList.get(2).trim());
                        b.setUserID(userID);
                        delList.add(b);
                    }

                }
            }
            userRoleService.ubBind(delList);
        }
        return new ModelAndView("redirect:goToBindProject.do");
    }
}
